package com.wbc.smartHome.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wbc.smartHome.dao.AdminDao;
import com.wbc.smartHome.model.Admin;
import com.wbc.smartHome.model.Menu;
import com.wbc.smartHome.model.SearchAdmin;
import com.wbc.smartHome.model.UpdatePasswordAdmin;
import com.wbc.smartHome.util.JWTUtil;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Objects;

@Service
@Transactional
public class AdminService {

    @Autowired
    private AdminDao adminDao;
    public PageInfo<Admin> AdminList(SearchAdmin searchAdmin) {
        /*List<Admin> admins = adminDao.adminList(searchAdmin);
        return admins;*/
        PageHelper.startPage(searchAdmin.getPageNo(), searchAdmin.getPageSize()); //1.会自动算出limit后面的开始位置
        List<Admin> admins =  adminDao.adminList(searchAdmin);                //2.重新发一条sql,查询总条数
        PageInfo<Admin> pageInfo = new PageInfo<>(admins);
        return  pageInfo;

    }

    public List<Menu> menus() {
        List<Menu> menus = adminDao.menus();
        return menus;
    }


    public void saveAdmin(Admin admin, String adminToken) {
        //1、解析token中的管理员id
        admin.setAdmin(JWTUtil.getAdmin(adminToken));//操作人封装进新增管理员

        //2、密码加密 截取电话后六位
        String password = admin.getPhone().substring(admin.getPhone().length()-6);//初始密码
        admin.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        //3、保存管理员
        adminDao.insertAdmin(admin);
        //4、保存管理员和分配的菜单的关系
        Integer[] menuIds = admin.getMenuIds();
        for (Integer menuId : menuIds) {
            adminDao.insertAdminMenu(menuId,admin.getId());
        }

    }


    @Test
    public void test() {
        System.out.println(DigestUtils.md5DigestAsHex("111".getBytes()));
    }

    public void deleteAdmin(int id) {
        adminDao.deleteAdminMenu(id);
        adminDao.deleteAdmin(id);
    }

    public Admin getAdminById(int id) {
        Admin admin = adminDao.getAdminById(id);
        //admin.setMenuIds(adminDao.getMenuIdsById(id));
        return admin;
    }

    public void updateAdmin(Admin admin, String adminToken) {
        //1、解析token中的管理员id
        admin.setAdmin(JWTUtil.getAdmin(adminToken));//操作人封装进新增管理员
        //2、修改保存的管理员信息
        adminDao.updataAdmin(admin);
        //3、修改管理员的管理权限
            //3.1 删除所有关系
            adminDao.deleteAdminMenu(admin.getId());
            //3.2 重新添加
            Integer[] menuIds = admin.getMenuIds();
            for (Integer menuId : menuIds) {
                adminDao.insertAdminMenu(menuId,admin.getId());
            }
    }

    public Admin getAdminByAccount(String account) {
        Admin admin = adminDao.getAdminByAccount(account);
        return admin;
    }

    public Boolean updatePassword(UpdatePasswordAdmin updatePasswordAdmin, String adminToken) {
        //1、解析token中的管理员id
        updatePasswordAdmin.setAdmin(JWTUtil.getAdmin(adminToken));//操作人封装进新增管理员
        //2、验证密码
        Admin admin = adminDao.getPasswordById(updatePasswordAdmin.getId());
        updatePasswordAdmin.setOldPassword(DigestUtils.md5DigestAsHex(updatePasswordAdmin.getOldPassword().getBytes()));
        //如果密码正确则保存
        if (Objects.equals(admin.getPassword(), updatePasswordAdmin.getOldPassword())){
            updatePasswordAdmin.setPassword(DigestUtils.md5DigestAsHex(updatePasswordAdmin.getNewPassword().getBytes()));
            adminDao.updatePassword(updatePasswordAdmin);
            return true;
        }
        //不争取不保存
        return false;

    }
}
